<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="keywords" content="">
<meta name="description" content="">
<meta name="author" content="">
<title></title>
<!--    <link href="assets/css/icons.css" rel="stylesheet"> -->
<!-- <link href="assets/css/bootstrap.min.css" rel="stylesheet"> -->
<link href="assets/css/style.css" rel="stylesheet">
<!--     <link href="assets/css/responsive.css" rel="stylesheet"> -->

</head>
<body>

	<form id="rewardForm">
		<h2 class="header-title text-center" id="title">添加部门信息</h2>
		<div class="form-group">
			<input id="rContent" name="rContent" type="text" class="form-control"
				placeholder="奖惩内容">
		</div>
		<div class="form-group">
			<input id="rType" name="rType" type="text" class="form-control"
				placeholder="奖惩类型">
		</div>
		<div class="form-group">
			<input id="rMoney" name="rMoney" type="text" class="form-control" placeholder="金额">
		</div>
		<div class="form-group">
			<input id="rTime" name="rTime" type="text" class="form-control" placeholder="时间">
		</div>
		<div class="form-group">
			<input type="button" id="confirmReward" value="确定"
				class="btn btn-primary btn-block"> <input type="button"
				id="returnReward" value="返回" class="btn btn-primary btn-block">
		</div>
	</form>

	<script>
		$(function(){
			//日期插件
			laydate.render({
				elem : '#rTime' //指定元素
			});
// 			$("#title").html("修改部门信息");
			//截取参数
			if(rewardId!=null){
				
				$.ajax({
					type : "get",
					url : "/HMS/queryRewardById",
					dataType : 'json',
					data:{"rId":rewardId},
					contentType : "application/json;charset=UTF-8",
					success : function(obj) {
						$("#rContent").val(obj.Obj.data[0].rContent);
						$("#rType").val(obj.Obj.data[0].rType);
						$("#rMoney").val(obj.Obj.data[0].rMoney);
						$("#rTime").val(new Date(obj.Obj.data[0].rTime).toLocaleString());
					},
					error : function(data) {
						alert('连接失败');
					}
				});
			}
			
			
			//确认按钮
			

			$("#confirmReward").click(function(){
				//数据校验
				checkDeptData();
				//启动校验
				$('#rewardForm').bootstrapValidator('validate');
				
				if(!$('#rewardForm').data('bootstrapValidator').isValid()){
					return;
				}
				//添加新的奖惩
				var url = "";
				if(!rewardId){
					url = "/HMS/addRewardInfo";
				}else{
					url = "/HMS/updateRewardInfo";
				}
				debugger;
				$.ajax({
					type : "get",
					url : url,
					dataType : 'json',
					data:{
						"id":rewardId,
						"rContent":$("#rContent").val(),
						"rType":$("#rType").val(),
						"rMoney":$("#rMoney").val(),
						"rTime":dateToString($("#rTime").val()),},
					contentType : "application/json;charset=UTF-8",
					success : function(value) {
						if(value.success == "OK"){
// 							addTable();
							alert("操作成功");
							
						}else{
							//登陆失败
							alert(value.msg);
						}
					},
					error : function(data) {
						alert("网络异常，请稍后重试");
					}
				});
			});
			//返回按钮
			$("#returnReward").click(function(){
				initReward();
			});
			
			
		});
		
		function checkDeptData(){
			$('#rewardForm').bootstrapValidator({
			　     message: '输入值错误，请重新输入',
			   feedbackIcons:{
				   validating : 'glyphicon glyphicon-refresh'
			   },
			   fields: {
				   rContent : {
			          message: '奖惩内容验证失败',
			          validators: {
			               notEmpty: {
			                     message: '奖惩内容不能为空'
			               }
			          }
			       },
			       rType : {
			          message: '奖惩类型验证失败',
			          validators: {
			               notEmpty: {
			                     message: '奖惩类型不能为空'
			               }
			          }
			       },
			       rMoney : {
				          message: '金额验证失败',
				          validators: {
				               notEmpty: {
				                     message: '金额不能为空'
				               }
				          }
				       },
				       rTime : {
				    	   message: '时间验证失败',
					          validators: {
					               notEmpty: {
					                     message: '时间不能为空'
					               }
					          }
				       }
			   
			   
			    }
		    });
		}
		
		function dateToString(str){
			var empBirthday = new Date(str).toLocaleString();
			empBirthday = empBirthday.substring(0,empBirthday.indexOf(" "));
			var list = [];
			for(var i=0;i<empBirthday.length;i++){
				if(empBirthday[i] == "/"){
					list[i] = "-";
				}else{
					list[i] = empBirthday[i];
				}
			}
			return list.join("");
		}
	</script>
</body>

</html>
